package com.arnold.learn.couchbase;

import com.couchbase.client.java.Bucket;
import com.couchbase.client.java.CouchbaseCluster;
import com.couchbase.client.java.document.JsonDocument;
import com.couchbase.client.java.document.json.JsonArray;
import com.couchbase.client.java.document.json.JsonObject;
import com.couchbase.client.java.query.N1qlQuery;
import com.couchbase.client.java.query.N1qlQueryResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by arnold.zhu on 2017/7/27.
 */
public class CouchbaseUtil {

    private static Logger logger = LoggerFactory.getLogger(CouchbaseUtil.class);

    public static N1qlQueryResult getQueryResult(String bucket, String query) {
        N1qlQueryResult result = null;

        // Connect to the bucket and open it
        Bucket couchBucket = CouchbaseFactory.getCouchbaseCluster().openBucket(bucket);
        // Create a N1QL Primary Index (but ignore if it exists)
        couchBucket.bucketManager().createN1qlPrimaryIndex(true, false);
        // Perform a N1QL Query
        result = couchBucket.query(
                N1qlQuery.simple(query)
        );

        closeBucket(couchBucket);

        return result;
    }

    public static N1qlQueryResult getQueryResult(String bucket, String query, String Condition) {
        N1qlQueryResult result = null;

        // Connect to the bucket and open it
        Bucket couchBucket = CouchbaseFactory.getCouchbaseCluster().openBucket(bucket);
        // Create a N1QL Primary Index (but ignore if it exists)
        couchBucket.bucketManager().createN1qlPrimaryIndex(true, false);
        // Perform a N1QL Query
        result = couchBucket.query(
                N1qlQuery.parameterized(query, JsonArray.from(Condition))
        );

        closeBucket(couchBucket);

        return result;
    }

    public static void upsertDocument(Bucket bucket, String documentId, JsonObject json) {
        bucket.upsert(JsonDocument.create(documentId, json));
        logger.info("document created with ID: {}, in bucket: {}", documentId, bucket);
    }

    public static void deleteDocument(String bucket, String documentId) {
        Bucket delBucket = CouchbaseFactory.getCouchbaseCluster().openBucket(bucket);
        delBucket.remove(documentId);
    }

    public static JsonDocument getDocumentById(String bucket, String documentId) {
        Bucket getBucket = CouchbaseFactory.getCouchbaseCluster().openBucket(bucket);
        return getBucket.get(documentId);
    }

    public static Bucket openBucket(CouchbaseCluster cluster, String bucket) {
        return cluster.openBucket(bucket);
    }

    public static void closeBucket(Bucket bucket) {
        bucket.close();
    }

    public static void disconnect(CouchbaseCluster cluster) {
        cluster.disconnect();
    }

    public static final CouchbaseCluster getCluster() {
        return Cluster.instance;
    }

    private static class Cluster {
        private static final CouchbaseCluster instance = CouchbaseFactory.getCouchbaseCluster();
    }

}
